package com.example.esc_im_client;

import android.app.Application;

import com.alipay.mobile.antcube.handler.CKImageHandler;
import com.antfin.cube.antcrystal.api.CExceptionInfo;
import com.antfin.cube.antcrystal.api.CExceptionListener;
import com.antfin.cube.antcrystal.api.CubeEngineConfig;
import com.antfin.cube.antcrystal.api.CubeModuleModel;
import com.antfin.cube.antcrystal.api.CubeWidgetInfo;
import com.mpaas.core.MP;
import com.mpaas.core.MPInitParam;
import com.mpaas.cube.adapter.api.CubeInitParam;
import com.example.esc_im_client.ui.home.CustomCubeModule;

import com.mpaas.cube.*;
import com.mpaas.mas.adapter.api.MPLogger;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

/**
 * @ClassName: App
 * @Description:
 * @Author: 墨渍
 * @Date: 2024/3/5 17:09
 **/
public class App extends Application {

    private static final String TAG = "MainApplication";

    public static App appContext;
    @Override
    public void onCreate() {
        super.onCreate();
        // 设置 cube 初始化参数
        appContext = this;
        CubeInitParam cubeInitParam
                = CubeInitParam.getDefault()
                .setCubeEngineConfig(generateCubeEngineConfig())
                // 注册卡片 to 客户端通道
                .setCubeModuleModels(generateModuleModel())
                // 注册自定义标签
//                .setCubeWidgetInfos(generateWidget())
                .setForceInitCubeDebug(true);
        // 初始化 mPaaS
        MP.init(this, MPInitParam.obtain().addComponentInitParam(cubeInitParam));
    }

    // 设置引擎参数
    private CubeEngineConfig generateCubeEngineConfig() {
        CubeEngineConfig config = new CubeEngineConfig();
        // 这里设置预置卡片路径，例如设置"cube"，则最终卡片预置路径为assets/cube/
        config.setResourcePath("cube");
        // 设置异常监听
        config.setExceptionListener(new CExceptionListener() {
            @Override
            public void onException(CExceptionInfo cExceptionInfo) {
                MPLogger.error(TAG, cExceptionInfo.getException());
            }
        });
        // 设置图片下载库，mpaas默认带，可以移除【魔方卡片-图片下载】组件，替换成您自己的图片下载库
        config.setImageHandler(new CKImageHandler(){
            @Override
            public String loadImage(String url, int width, int height, Map<String, Object> params, LoadImageListener loadImageListener) {
                return super.loadImage(url, width, height, params, loadImageListener);
            }
        });
        return config;
    }

    /**
     * 卡片调用客户端方法
     */
    public Collection<CubeModuleModel> generateModuleModel(){
        Collection<CubeModuleModel> cubeModuleModels = new LinkedList<>();
        cubeModuleModels.add(new CubeModuleModel("custom", CustomCubeModule.class.getName(), new String[]{"cubeToClient"}));
//        CubeService.instance().getEngine().registerModule(cubeModuleModels, null);
        return cubeModuleModels;
    }
}
